package easy;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution_404 {

    public int sumOfLeftLeaves(TreeNode root) {
//        return digui(root);
        MyInteger sum = new MyInteger();
        digui2(root, sum);
        return sum.val;
    }

    public int digui(TreeNode node) {
        if (node == null) {
            return 0;
        } else {
            System.out.println(node.left==null?0:node.left.val);
            int val = 0;
            if (node.left!=null && node.left.left==null && node.left.right == null) {
                val = node.left.val;
            }
            System.out.println(val);
            return val + digui(node.left) + digui(node.right);
        }
    }

    public void digui2(TreeNode node, MyInteger sum) {
        if (node == null) {
            return;
        } else {
            if (node.left!=null && node.left.left==null && node.left.right == null) {
                sum.val += node.left.val;
                System.out.println(sum.val);
            }
            digui2(node.left, sum);
            digui2(node.right, sum);
        }
    }

    public static class MyInteger {
        public int val = 0;
    }

    public static void main(String[] args) {
        Solution_404 main = new Solution_404();
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
//        root.left = new TreeNode(2);
//        root.right = new TreeNode(3);
//        root.left.left = new TreeNode(4);
//        root.left.right = new TreeNode(5);
        System.out.println(main.sumOfLeftLeaves(root));
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}